<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>回到网页顶部</title>
    <style>
      body {
        height: 10000px;
      }
      .btn {
        height: 40px;
        width: 80px;
        border: 3px solid #000;
        display: flex;
        justify-content: center;
        align-items: center;
        font-weight: bold;
        cursor: pointer;
        position: fixed;
        right: 10px;
        bottom: 100px;
      }

      .hidden {
        display: none !important;
      }
    </style>
  </head>
  <body>
    <h1>长江二桥</h1>
    <div class="btn hidden">回到顶部</div>
    <script>
      let btnEle = document.querySelector('.btn')

      document.addEventListener('scroll', function () {
        let htmlScrollTop =
        document.documentElement.scrollTop || document.body.scrollTop
        if (htmlScrollTop > 100) {
          btnEle.classList.remove('hidden')
        } else {
          btnEle.classList.add('hidden')
        }
      })

      btnEle.addEventListener('click', function () {
        document.documentElement.scrollTop = document.body.scrollTop = 0
      })
    </script>
  </body>
</html>
